﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Noesis.Javascript;


namespace Kugar.Core.HeadlessBrowser
{
   
    public class HtmlElementBase:HtmlAgilityPack.HtmlNode
    {
        public HtmlElementBase(HtmlDocument ownerdocument, int index) : base(HtmlNodeType.Element, ownerdocument, index)
        {
            Owner = ownerdocument;
        }

        public HtmlDocument Owner { set; get; }

        public string alt
        {
            get { return getPropertyValue("alt"); }
            set { setPropertyValue("alt", value); }
        }

        public string id
        {
            get { return getPropertyValue("id"); }
            set { setPropertyValue("id", value); }
        }

        public string className
        {
            get { return getPropertyValue("className"); }
            set { setPropertyValue("className",value); }
        }

        public string lang
        {
            get { return getPropertyValue("lang"); }
            set { setPropertyValue("lang", value); }
        }

        public string title
        {
            get { return getPropertyValue("title"); }
            set { setPropertyValue("title", value); }
        }

        public string onclick { get; set; }

        public string @class { get; set; }

        public virtual void click()
        {
            if (disabled=="false" && onclick != null && !string.IsNullOrWhiteSpace(onclick) && Click != null)
            {
                try
                {
                    Click(this, EventArgs.Empty);
                }
                catch (Exception)
                {
                    
                }
                
            }
        }

        public string disabled { set; get; }

        public event EventHandler Click;

        protected string getPropertyValue(string propName)
        {
            return base.GetAttributeValue(propName, "");
        }

        protected void setPropertyValue(string propName, string value)
        {
            base.SetAttributeValue(propName, value);
        }
    }

    public class HtmlForm:HtmlElementBase
    {
        public HtmlForm(HtmlDocument ownerdocument, int index) : base(ownerdocument, index)
        {
        }

        private string _target = "_self";
        public string target
        {
            set 
            {
                var v = value.ToLower();

                if (v != "_blank" && v != "_parent" && v != "_self" && v != "_top ")
                {
                    return;
                }

                _target = v;
            }
            get { return _target; }
        }

        private string _method="get";
        public string method
        {
            get { return _method; }
            set 
            {
                var v = value.ToLower();

                if (v != "get" && v != "post")
                {
                    return;
                }

                _method = v;
            }
        }

        private string _enctype = "application/x-www-form-urlencoded";
        public string enctype
        {
            get { return _enctype; }
            set
            {
                var v = value.ToLower();
                if (v != "application/x-www-form-urlencoded" && v != "multipart/form-data")
                {
                    return;
                }

                _enctype = v;
            }
        }

        public void reset()
        {
            var nodelist = this.ChildNodes;

            if (nodelist.Count<=0)
            {
                return;
            }

            foreach (var node in nodelist)
            {
                if (!(node is HtmlInputBase))
                {
                    continue;
                }

                if (node is HtmlInputCheckbox)
                {
                    var temp=((HtmlInputRadio)node);
                    temp.@checked = temp.defaultChecked;
                }
                

            }

        }

        public void submit()
        {
            
        }

        public event EventHandler ResetValue;
    }

    public class HtmlInputBase:HtmlElementBase
    {
        protected HtmlForm _form = null;

        public HtmlInputBase(HtmlDocument ownerdocument, int index) : base(ownerdocument, index)
        {
            if (ownerdocument==null)
            {
                throw new ArgumentNullException("ownerdocument");
            }
        }

        public HtmlInputBase(HtmlForm ownerForm, int index)
            : base(ownerForm.OwnerDocument, index)
        {
            if (ownerForm==null)
            {
                throw new ArgumentNullException("ownerForm");
            }
            _form = ownerForm;
        }

        public HtmlForm form
        {
            get { return _form; }
        }

        public string value
        {
            get { return getPropertyValue("value"); }
            set { setPropertyValue("value", value); }
        }

        public string name
        {
            get { return getPropertyValue("name"); }
            set { setPropertyValue("name", value); }
        }

        public virtual string type
        {
            get { return "input"; }
        }

        public string accessKey
        {
            get { return getPropertyValue("accessKey"); }
            set { setPropertyValue("accessKey", value); }
        }

        public void blur()
        {

        }

        public void focus()
        {

        }

    }

    public class HtmlInputButton : HtmlInputBase
    {
        public HtmlInputButton(HtmlDocument ownerdocument, int index) : base(ownerdocument, index)
        {
        }

        public HtmlInputButton(HtmlForm ownerForm, int index)
            : base(ownerForm.OwnerDocument, index)
        {
            
        } 


        public override string type
        {
            get { return "checkbox"; }
        }



    }

    public class HtmlInputCheckbox:HtmlInputBase
    {
        public HtmlInputCheckbox(HtmlDocument ownerdocument, int index) : base(ownerdocument, index)
        {
        }

        public HtmlInputCheckbox(HtmlForm ownerForm, int index) : base(ownerForm, index)
        {
        }

        public virtual bool @checked
        {
            get { return bool.Parse(getPropertyValue("checked")); }
            set { setPropertyValue("checked", value.ToString()); }
        }

        public bool defaultChecked
        {
            get { return false; }
        }
    }

    public class HtmlInputRadio : HtmlInputCheckbox
    {
        public HtmlInputRadio(HtmlDocument ownerdocument, int index) : base(ownerdocument, index)
        {
        }

        public HtmlInputRadio(HtmlForm ownerForm, int index) : base(ownerForm, index)
        {
        }

        public override bool @checked
        {
            get { return bool.Parse(getPropertyValue("checked")); }
            set
            {
                setPropertyValue("checked", value.ToString());

                if (value)
                {
                    if (this.ParentNode == null || this.ParentNode.ChildNodes.Count <= 0)
                    {
                        return;
                    }

                    foreach (var childNode in this.ParentNode.ChildNodes)
                    {
                        if (childNode is HtmlInputRadio && childNode !=this)
                        {
                            ((HtmlInputRadio) childNode).@checked = false;
                        }
                    }                    
                }
            }
        }

        public override string type
        {
            get
            {
                return "radio";
            }
        }
    }

    public class HtmlInputReset:HtmlInputBase
    {
        public HtmlInputReset(HtmlDocument ownerdocument, int index) : base(ownerdocument, index)
        {
        }

        public HtmlInputReset(HtmlForm ownerForm, int index) : base(ownerForm, index)
        {
        }

        public override void click()
        {
            if (this.form!=null)
            {
                this.form.reset();
            }
            base.click();
        }
    }
}
